﻿using System;
using System.Windows.Forms;
using UniMana.Utils.Data;
using DevExpress.XtraBars;
using System.Data;
using System.Linq;
using DevExpress.XtraEditors;
using System.Collections.Generic;
//using UniMana.Lib.Score;
using UniMana.Utils.Controls;
//using UniMana.Lib.Catalogue;
using UniMana.Data;
using DevExpress.XtraGrid.Views.BandedGrid;
using UniMana.Utils.Helper;
using UniMana.Utils.Configs;

namespace UniMana.Win.Training
{
    public partial class FrmScore_SubTransScripts : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        private bds m_bds = new bds();
        //private DBTraining m_Catalogue = new DBTraining();
        private DataTable m_dt;
        private ClsReadNumber m_readnumber = new ClsReadNumber();
        private DataTable m_dt1 = new DataTable();
        public bool m_bCheckPrint = false;
        public FrmScore_SubTransScripts()
        {
            InitializeComponent();
            LoadData();
            InitControlEvent();
        }
        public FrmScore_SubTransScripts(string subjectClassID, DataTable dt,bool checkPrint, string credit="")
        {
            InitializeComponent();
            m_dt1 = dt;
            m_bCheckPrint = checkPrint;
            LoadData();
            InitControlEvent();
            txtSubjectClassID.Text = subjectClassID;
            try
            {
                bgrvScore.FocusedRowHandle = 2;
                Training_Subject_ClassInfo info = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
                Catalogue_TermInfo infoTerm = new Catalogue_TermInfo(Convert.ToInt32(info.Properties.Catalogue_TermKey));
                Catalogue_SchoolYearInfo infoSchoolYear = new Catalogue_SchoolYearInfo(Convert.ToInt32(info.Properties.Catalogue_SchoolYearKey));
                Training_SubjectInfo infoSubject = new Training_SubjectInfo(Convert.ToInt32(info.Properties.Training_SubjectKey));
                Catalogue_DepartmentInfo infoDep = new Catalogue_DepartmentInfo(Convert.ToInt32(infoSubject.Properties.Catalogue_DepartmentKey));
                RprScore_Transcripts rp = new RprScore_Transcripts(txtLevel.Text, txtSubject.Text, infoDep.Properties.Name, 
                                                                    infoTerm.Properties.Name.ToString(), infoSchoolYear.Properties.Name.ToString(),
                                                                    m_dt.Copy(), bgrvScore, txtMajor.Text, txtSubjectClassID.Text, credit);
                rp.ShowPreviewDialog();
            }
            catch { }
        }
        public void LoadData()
        {
            lueCatalogue_Term.GetData(m_data.m_DCReadOnly.Catalogue_Terms);
            lueCatalogue_SchoolYear.GetData(m_data.m_DCReadOnly.Catalogue_SchoolYears);
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.Where(p => p.SubjectManage.ToString() == "true"));
            bgrvScore.Bands.Clear();
        }
        public void InitControlEvent()
        {
            xucBar2.btnAddNew_Click += new Utils.Controls.xucBar.ButtonClickHandler(this.btnAddNew_Click);
            //xucBar2.btnSave_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnSave_ItemClick);
            xucBar2.btnRefresh_Click += new Utils.Controls.xucBar.ButtonClickHandler(this.btnRefresh_ItemClick);
            xucBar2.btnExport_Click += new Utils.Controls.xucBar.ButtonClickHandler(this.btnExportExcel_ItemClick);
            xucBar2.btnPrint_Click += new Utils.Controls.xucBar.ButtonClickHandler(this.btnPrint_ItemClick);
            //xucBar2.btnEdit_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnEdit_Click);
            //xucBar2.btnCancel_Click += new UniMana.Controls.xucBar.ButtonClickHandler(btnCancel_Click);
            //xucBar2.btnImport_Click += new UniMana.Controls.xucBar.ButtonClickHandler(btnImportExcel_ItemClick);
        }
        //Buttons
        private void btnRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            reLoad();
        }
        private void btnAddNew_Click(object sender, ItemClickEventArgs e)
        {
            //for (int i = 1; i < advBandedGridView1.Bands.Count - 1; i++)
            //{
            //    for (int j = 0; j < advBandedGridView1.RowCount; j++)
            //    {
            //        advBandedGridView1.SetRowCellValue(j, advBandedGridView1.Bands[i].Columns[1], 2);

            //        advBandedGridView1.SetRowCellValue(j, advBandedGridView1.Bands[i].Columns[2], m_readnumber.Convert(advBandedGridView1.GetRowCellValue(j, advBandedGridView1.Bands[i].Columns[1]).ToString(), '.', "chấm"));
            //    }
            //}
            //MessageBox.Show("Suceess!");
        }
        private void btnExportExcel_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bgrvScore.RowCount > 0)
            {
                int subjectClassKey;
                if (lueTraining_Subject_Class.EditValue != null)
                {
                    subjectClassKey = lueTraining_Subject_Class.GetIntValue();
                }
                else
                {
                    Training_Subject_ClassInfo info = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
                    subjectClassKey = info.Properties.Training_Subject_ClassKey;
                }
                this.bgrvScore.BeginDataUpdate();
                this.bgrvScore.BeginUpdate();
                if (m_data.m_DCReadOnly.Training_Scores.Check(subjectClassKey) == true)
                {
                    outputScore(subjectClassKey);
                }
                else
                {
                    inputScore(subjectClassKey);
                }
                for (int i = 0; i < bgrvScore.Columns.Count; i++)
                {
                    bgrvScore.Columns[i].OptionsColumn.AllowMove = false;
                }
                this.bgrvScore.EndUpdate();
                this.bgrvScore.EndDataUpdate();

                Training_Subject_ClassInfo classID = new Training_Subject_ClassInfo(subjectClassKey);

                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.FileName = "Bảng Điểm Lớp_" + classID.Properties.ID.ToString() + "_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_');
                saveFile.Title = "Xuất ra tệp tin";
                saveFile.DefaultExt = "xls";
                saveFile.Filter = "Excel 2003 (*.xls)|";
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    bgrvScore.ExportToXls(saveFile.FileName);
                }
            }
        }
        private void btnPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bgrvScore.RowCount > 2)
            {
                bgrvScore.FocusedRowHandle = 2;
                if (lueCatalogue_Department.EditValue != null && lueCatalogue_SchoolYear.EditValue != null && lueCatalogue_Term.EditValue != null && lueTraining_Subject_Class.EditValue != null)
                {
                    Training_Subject_ClassInfo info = new Training_Subject_ClassInfo(lueTraining_Subject_Class.GetIntValue());
                    string subjectClassID = info.Properties.ID.ToString();
                    RprScore_Transcripts rp = new RprScore_Transcripts(txtLevel.Text, txtSubject.Text, lueCatalogue_Department.Text, lueCatalogue_Term.Text, lueCatalogue_SchoolYear.Text, m_dt.Copy(), bgrvScore, txtMajor.Text, subjectClassID, txtCredits.Text);
                    rp.ShowPreviewDialog();
                }
                else
                {
                    try
                    {
                        Training_Subject_ClassInfo info = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
                        Catalogue_TermInfo infoTerm = new Catalogue_TermInfo(Convert.ToInt32(info.Properties.Catalogue_TermKey));
                        Catalogue_SchoolYearInfo infoSchoolYear = new Catalogue_SchoolYearInfo(Convert.ToInt32(info.Properties.Catalogue_SchoolYearKey));
                        Training_SubjectInfo infoSubject = new Training_SubjectInfo(Convert.ToInt32(info.Properties.Training_SubjectKey));
                        Catalogue_DepartmentInfo infoDep = new Catalogue_DepartmentInfo(Convert.ToInt32(infoSubject.Properties.Catalogue_DepartmentKey));
                        RprScore_Transcripts rp = new RprScore_Transcripts(txtLevel.Text, txtSubject.Text, infoDep.Properties.Name, infoTerm.Properties.Name.ToString(), infoSchoolYear.Properties.Name.ToString(), m_dt.Copy(), bgrvScore, txtMajor.Text, txtSubjectClassID.Text, txtCredits.Text);
                        rp.ShowPreviewDialog();
                    }
                    catch { }
                }
            }
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            if (txtSubjectClassID != null && txtSubjectClassID.Text != "")
            {
                Training_Subject_ClassInfo infoClass = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
                if (infoClass.Properties.Training_Subject_ClassKey != 0)
                {
                    getInfoFinal();
                }
                else
                {
                    FrmScore_ShowSubjectClassID f = new FrmScore_ShowSubjectClassID(txtSubjectClassID.Text,true);
                    if (f.Check())
                    {
                        f.ShowDialog();
                        txtSubjectClassID.Text = f.m_sClassID;
                    }
                    else
                    {
                        XtraMessageBox.Show("Mã lớp học phần không tồn tại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
        //LookupEdits
        private void lueCatalogue_SchoolYear_EditValueChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        private void lueCatalogue_Term_EditValueChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        private void lueTraining_Subject_Class_EditValueChanged(object sender, EventArgs e)
        {
            if (lueCatalogue_SchoolYear.EditValue != null && lueCatalogue_Term.EditValue != null && lueCatalogue_Department.EditValue != null)
            {
                txtCredits.Text = null;
                txtLevel.Text = null;
                txtMajor.Text = null;
                txtSubject.Text = null;
                txtSubjectID.Text = null;
                getAllInfo(lueTraining_Subject_Class.GetIntValue());
            }
        }
        //GridView
        private void bgrvScore_Click(object sender, EventArgs e)
        {
            //if (bgrvScore.RowCount > 0)
            //{
            //    int sClassKey = getSubjectClassKey();
            //    Training_ScoreInfo info = new Training_ScoreInfo();
            //    double credit = info.TotalCredit(sClassKey, bgrvScore.GetFocusedRowCellDisplayText(bgrvScore.Columns[1]).ToString());
            //    txtCredits.Text = credit.ToString();
            //}
        }
        //Methods
        private void getInfoFinal()
        {
            if (txtSubjectClassID != null)
            {
                if (txtSubjectClassID.Text != "")
                {
                    lueCatalogue_Department.EditValue = null;
                    lueCatalogue_SchoolYear.EditValue = null;
                    lueCatalogue_Term.EditValue = null;
                    lueTraining_Subject_Class.Properties.DataSource = null;
                    lueTraining_Subject_Class.Properties.ImmediatePopup = false;
                    lueTraining_Subject_Class.EditValue = null;

                    Training_Subject_ClassInfo infoClass = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
                    int sClassKey = infoClass.Properties.Training_Subject_ClassKey;
                    getAllInfo(sClassKey);
                }
            }
            else
            {
                reLoad();
            }
        }
        private void getAllInfo(int subjectClassKey)
        {
             try
                {
                    txtCredits.Text = null;
                    txtLevel.Text = null;
                    txtMajor.Text = null;
                    txtSubject.Text = null;
                    txtSubjectID.Text = null;

                    Training_Subject_ClassInfo infoCredits = new Training_Subject_ClassInfo(subjectClassKey);
                    DataTable dtCredits = infoCredits.TotalCredits(subjectClassKey).ToDataTable();
                    txtCredits.Text = dtCredits.Rows[0]["TotalCredits"].ToString();

                    Training_Subject_ClassInfo infoClass = new Training_Subject_ClassInfo(subjectClassKey);
                    Training_SubjectInfo infoSubject = new Training_SubjectInfo(infoClass.Properties.Training_SubjectKey.Value);
                    txtSubject.Text = infoSubject.Properties.Name.ToString();
                    txtSubjectID.Text = infoSubject.Properties.ID.ToString();
                    string level = infoSubject.Properties.ID.Substring(0, 1);
                    switch (level)
                    {
                        case "C":
                            txtLevel.Text = "Cao đẳng";
                            break;
                        case "D":
                            txtLevel.Text = "Đại học";
                            break;
                        case "T":
                            txtLevel.Text = "Trung cấp";
                            break;
                        default:
                            txtLevel.Text = "";
                            break;
                    }
                    //lay cac nganh
                    DataTable dt = new DataTable();
                    dt = m_data.m_DCReadOnly.Training_Student_Classes.GetMajorList(m_data.m_DCReadOnly.Training_Student_Subject_Classes,
                        m_data.m_DCReadOnly.Training_Students, subjectClassKey).ToDataTable();
                    if (dt.Rows.Count > 0)
                    {
                        string major = "";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Catalogue_MajorInfo info = new Catalogue_MajorInfo(int.Parse(dt.Rows[i]["Catalogue_MajorKey"].ToString()));
                            if (dt.Rows.Count == 1)
                            {
                                major += info.Properties.Name.ToString();
                            }
                            else
                            {
                                if (i != dt.Rows.Count-1)
                                {
                                    major += info.Properties.Name.ToString() + ", ";
                                }
                                else
                                {
                                    major += info.Properties.Name.ToString();
                                }
                            }
                        }
                        txtMajor.Text = major;
                    }
                }
                catch { }
            this.bgrvScore.BeginDataUpdate();
            this.bgrvScore.BeginUpdate();
            if (m_bCheckPrint == true)
            {
                if (m_data.m_DCReadOnly.Training_Scores.Check(subjectClassKey) == true)
                {
                    outputScore(subjectClassKey);
                }
                else
                {
                    inputScore(subjectClassKey);
                }
            }
            else
            {
                inputScore(subjectClassKey);
            }
            for (int i = 0; i < bgrvScore.Columns.Count; i++)
            {
                bgrvScore.Columns[i].OptionsColumn.AllowMove = false;
            }
            this.bgrvScore.EndUpdate();
            this.bgrvScore.EndDataUpdate();
        }
        private void reLoad()
        {
            lueCatalogue_Department.EditValue = null;
            lueCatalogue_SchoolYear.EditValue = null;
            lueCatalogue_Term.EditValue = null;
            lueTraining_Subject_Class.Properties.DataSource = null;
            lueTraining_Subject_Class.Properties.ImmediatePopup = false;
            lueTraining_Subject_Class.EditValue = null;

            txtSubjectClassID.Text = null;

            gctScore.DataSource = null;
            bgrvScore.PopulateColumns();
            bgrvScore.Bands.Clear();
            bgrvScore.Columns.Clear();

            txtCredits.Text = null;
            txtLevel.Text = null;
            txtMajor.Text = null;
            txtSubject.Text = null;
            txtSubjectClassID.Text = null;
            txtSubjectID.Text = null;

            txtSubjectClassID.Focus();
        }
        private void inputScore(int subjectClassID)
        {
            #region[Nhập điểm]
            //if (lueCatalogue_Term.EditValue != null && lueCatalogue_SchoolYear.EditValue != null && lueTraining_Subject_Class.EditValue != null && lueCatalogue_Department.EditValue != null)
            //{
                m_dt = new DataTable();
                m_dt.Columns.Add("SVKey");
                m_dt.Columns.Add("MSSV");
                m_dt.Columns.Add("HỌ LÓT");
                m_dt.Columns.Add("TÊN");
                m_dt.Columns.Add("NGÀY SINH");
                m_dt.Columns.Add("GIỚI TÍNH");
                //m_dt1 = m_Score.m_DCReadOnly.Training_Student_Subject_Classes.GetStudentsSubjectClass(m_Score.m_DCReadOnly.Training_Student_Classes,
                //                        m_Score.m_DCReadOnly.Training_Students, subjectClassID);
                for (int i = 0; i < m_dt1.Rows.Count; i++)
                {
                    DateTime date;
                    if (DateTime.TryParse(m_dt1.Rows[i]["Birthday"].ToString(), out date))
                    {
                        m_dt.Rows.Add(new object[] { m_dt1.Rows[i]["Training_StudentKey"].ToString(), m_dt1.Rows[i]["ID"].ToString(), 
                                                m_dt1.Rows[i]["LastName"].ToString(), m_dt1.Rows[i]["SureName"].ToString(), 
                                            date.ToString("dd/MM/yyyy"), m_dt1.Rows[i]["Gender"].ToString()});
                    }
                    else
                    {
                        try
                        {
                            m_dt.Rows.Add(new object[] { m_dt1.Rows[i]["Training_StudentKey"].ToString(), m_dt1.Rows[i]["ID"].ToString(), 
                                                m_dt1.Rows[i]["LastName"].ToString(), m_dt1.Rows[i]["SureName"].ToString(), 
                                            m_dt1.Rows[i]["Birthday"].ToString(), m_dt1.Rows[i]["Gender"].ToString()});
                        }
                        catch { }
                    }
                }
                m_dt.Columns.Add("STT", System.Type.GetType("System.Int32"));
                for (int i = 0; i < m_dt.Rows.Count; i++)
                {
                    m_dt.Rows[i]["STT"] = i+1;
                }
                List<int> stl = m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes.getListScoreTypeClass(m_data.m_DCReadOnly.Training_ScoreTypes,
                    subjectClassID);

                this.bgrvScore.Bands.Clear();
                this.bgrvScore.Columns.Clear();
                this.bgrvScore.Bands.Add(Info(ref this.bgrvScore));
                
                for (int i=0;i<stl.Count;i++)
                {
                    this.bgrvScore.Bands.Add(InSertScoreType(stl[i], ref this.bgrvScore, ref this.m_dt, subjectClassID));
                }

                if (stl.Count > 0)
                {
                    bgrvScore.Bands.AddBand("HỌC PHẦN");
                    BandedGridColumn clMark_number = new BandedGridColumn();
                    m_dt.Columns.Add("HPDiemSo");
                    clMark_number.Caption = "ĐIỂM SỐ";
                    clMark_number.FieldName = "HPDiemSo";
                    clMark_number.Width = 50;

                    BandedGridColumn clMark_char = new BandedGridColumn();
                    m_dt.Columns.Add("HPDiemChu");
                    clMark_char.Caption = "ĐIỂM CHỮ";
                    clMark_char.FieldName = "HPDiemChu";
                    clMark_char.Width = 100;

                    BandedGridColumn clMark_note = new BandedGridColumn();
                    m_dt.Columns.Add("HPGhiChu");
                    clMark_note.Caption = "GHI CHÚ";
                    clMark_note.FieldName = "HPGhiChu";
                    clMark_note.Width = 50;

                    bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns.Add(clMark_number);
                    bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns.Add(clMark_char);
                    bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns.Add(clMark_note);

                }

                for (int i = 0; i < this.bgrvScore.Columns.Count; i++)
                {
                    this.bgrvScore.Columns[i].Visible = true;
                }
                this.gctScore.DataSource = m_dt;

                //advBandedGridView1.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                //advBandedGridView1.Appearance.FocusedCell.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            //}
            #endregion
        }
        private void outputScore(int subjectClassKey)
        {
            #region[Xuất điểm]
            //if (lueCatalogue_Term.EditValue != null && lueCatalogue_SchoolYear.EditValue != null && lueTraining_Subject_Class.EditValue != null && lueCatalogue_Department.EditValue != null)
            //{
                bgrvScore.BeginUpdate();                
                m_dt = new DataTable();
                m_dt.Columns.Add("SVKey");
                m_dt.Columns.Add("MSSV");
                m_dt.Columns.Add("HỌ LÓT");
                m_dt.Columns.Add("TÊN");
                m_dt.Columns.Add("NGÀY SINH");
                m_dt.Columns.Add("GIỚI TÍNH");
                //m_dt1 = m_Score.m_DCReadOnly.Training_Student_Subject_Classes.GetStudentsSubjectClass(m_Score.m_DCReadOnly.Training_Student_Classes,
                //                        m_Score.m_DCReadOnly.Training_Students, subjectClassKey);
                for (int i = 0; i < m_dt1.Rows.Count; i++)
                {
                    DateTime date;
                    if (DateTime.TryParse(m_dt1.Rows[i]["Birthday"].ToString(), out date))
                    {
                        m_dt.Rows.Add(new object[] { m_dt1.Rows[i]["Training_StudentKey"].ToString(), m_dt1.Rows[i]["ID"].ToString(), 
                                                m_dt1.Rows[i]["LastName"].ToString(), m_dt1.Rows[i]["SureName"].ToString(), 
                                            date.ToString("dd/MM/yyyy"), m_dt1.Rows[i]["Gender"].ToString()});
                    }
                    else
                    {
                        try
                        {
                            m_dt.Rows.Add(new object[] { m_dt1.Rows[i]["Training_StudentKey"].ToString(), m_dt1.Rows[i]["ID"].ToString(), 
                                                m_dt1.Rows[i]["LastName"].ToString(), m_dt1.Rows[i]["SureName"].ToString(), 
                                            m_dt1.Rows[i]["Birthday"].ToString(), m_dt1.Rows[i]["Gender"].ToString()});
                        }
                        catch { }
                    }
                }
                m_dt.Columns.Add("STT", System.Type.GetType("System.Int32"));
                for (int i = 0; i < m_dt.Rows.Count; i++)
                {
                    m_dt.Rows[i]["STT"] = i + 1;
                }

                //List<int> stl = m_Score.m_DCReadOnly.Training_SubjectClass_ScoreTypes.getListScoreTypeClass(m_Score.m_DCReadOnly.Training_ScoreTypes, lueTraining_Subject_Class.GetIntValue());
                List<int> stl = m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes.getListScoreTypeClass(m_data.m_DCReadOnly.Training_ScoreTypes,
                    subjectClassKey);

                this.bgrvScore.Bands.Clear();
                this.bgrvScore.Columns.Clear();
                this.bgrvScore.Bands.Add(Info(ref this.bgrvScore));

                for (int i = 0; i < stl.Count; i++)
                {
                    this.bgrvScore.Bands.Add(InSertScoreType(stl[i], ref this.bgrvScore, ref this.m_dt, subjectClassKey));
                }
                           
                bgrvScore.Bands.AddBand("HỌC PHẦN");
                BandedGridColumn clMark_number = new BandedGridColumn();
                m_dt.Columns.Add("HPDiemSo");
                clMark_number.Caption = "ĐIỂM SỐ";
                clMark_number.FieldName = "HPDiemSo";
                clMark_number.Width = 50;

                BandedGridColumn clMark_char = new BandedGridColumn();
                m_dt.Columns.Add("HPDiemChu");
                clMark_char.Caption = "ĐIỂM CHỮ";
                clMark_char.FieldName = "HPDiemChu";
                clMark_char.Width = 100;

                BandedGridColumn clMark_note = new BandedGridColumn();
                m_dt.Columns.Add("HPGhiChu");
                clMark_note.Caption = "GHI CHÚ";
                clMark_note.FieldName = "HPGhiChu";
                clMark_note.Width = 50;

                bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns.Add(clMark_number);
                bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns.Add(clMark_char);
                bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns.Add(clMark_note);
                

                this.gctScore.DataSource = m_dt;
                //do du lieu vao
                for (int j = 0; j < stl.Count; j++)
                {
                    //DataTable scoreList = m_Score.m_DCReadOnly.Training_Scores.getScore(m_Score.m_DCReadOnly.Training_Students, lueTraining_Subject_Class.GetIntValue(), int.Parse(stl[j].ToString())).ToDataTable();
                    DataTable scoreList = m_data.m_DCReadOnly.Training_Scores.getScore(
                                          m_data.m_DCReadOnly.Training_Students, m_data.m_DCReadOnly.Training_Student_Classes,
                                          m_data.m_DCReadOnly.Training_Student_Subject_Classes, m_data.m_DCReadOnly.Training_Subject_Classes,
                                          subjectClassKey, int.Parse(stl[j].ToString())).ToDataTable();
                    if (m_data.m_DCReadOnly.Training_Scores.CheckSubjectClassScoreType(subjectClassKey, stl[j]) == true)
                    {
                        for (int k = 0; k < bgrvScore.RowCount; k++)
                        {
                            for (int l = 0; l < scoreList.Rows.Count; l++)
                            {
                                if (bgrvScore.GetRowCellValue(k,"SVKey").ToString() == scoreList.Rows[l]["Training_StudentKey"].ToString())
                                {
                                    //5-9
                                    bgrvScore.SetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1], scoreList.Rows[l][2].ToString());
                                    string s = m_readnumber.Convert(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString(), '.', "chấm");
                                    bgrvScore.SetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[2], UppercaseFirst(s));
                                }
                            }
                        }
                    }
                    else
                    {
                        //for (int k = 0; k < bgrvScore.RowCount; k++)
                        //{
                        //    bgrvScore.SetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1], scoreList.Rows[k][2].ToString());
                        //    //bgrvScore.SetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[2], m_readnumber.Convert(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString(), '.', "chấm"));
                        //}
                    }
                }
                //////////
                bgrvScore.BeginUpdate();
                List<double> percent = new List<double>();
                for (int i = 0; i < stl.Count; i++)
                {
                    //Training_SubjectClass_ScoreTypeInfo info = new Training_SubjectClass_ScoreTypeInfo(lueTraining_Subject_Class.GetIntValue(), int.Parse(stl[i].ToString()));
                    Training_SubjectClass_ScoreTypeInfo info = new Training_SubjectClass_ScoreTypeInfo(subjectClassKey, int.Parse(stl[i].ToString()));
                    percent.Add(double.Parse(info.Properties.DefaultPercent.ToString()));
                }
                double scoreFinal = 0;
                //if (checkScoreType(stl, lueTraining_Subject_Class.GetIntValue()))
                Training_Subject_ClassInfo infoclass =  new Training_Subject_ClassInfo(subjectClassKey);
                if (infoclass.Properties.Yearly == true)
                {
                    if (checkScoreType(stl, subjectClassKey))
                    {
                        for (int k = 0; k < bgrvScore.RowCount; k++)
                        {
                            for (int j = 0; j < percent.Count; j++)
                            {
                                if (stl[j].ToString() == DefaultConfig.SCORETYPE_3)
                                {
                                    if (bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]) != null)
                                    {
                                        if (bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString() != "")
                                        {
                                        
                                            try
                                            {
                                            scoreFinal += (double.Parse(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString()) * (percent[j]) / 100)
                                                - (double.Parse(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j].Columns[1]).ToString()) * (percent[j]) / 100);
                                            }
                                            catch{}
                                        }
                                    }
                                }
                                else
                                {
                                    if (bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]) != null)
                                    {
                                        if (bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString() != "")
                                        {
                                            scoreFinal += (double.Parse(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString()) * (percent[j]) / 100);
                                        }
                                    }
                                }
                            }
                            bgrvScore.SetRowCellValue(k, bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns[0], Math.Round(scoreFinal, 0));
                            string s = m_readnumber.Convert(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns[0]).ToString(), '.', "chấm");
                            bgrvScore.SetRowCellValue(k, bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns[1], UppercaseFirst(s));
                            scoreFinal = 0;
                        }
                    }
                }
                else
                {
                    if (checkScoreType(stl, subjectClassKey))
                    {
                        for (int k = 0; k < bgrvScore.RowCount; k++)
                        {
                            for (int j = 0; j < percent.Count; j++)
                            {
                                if (bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]) != null)
                                {
                                    if (bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString() != "")
                                    {
                                        scoreFinal += (double.Parse(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[j + 1].Columns[1]).ToString()) * (percent[j]) / 100);
                                    }
                                }
                            }
                            bgrvScore.SetRowCellValue(k, bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns[0], Math.Round(scoreFinal, 1));
                            string s = m_readnumber.Convert(bgrvScore.GetRowCellValue(k, bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns[0]).ToString(), '.', "chấm");
                            bgrvScore.SetRowCellValue(k, bgrvScore.Bands[bgrvScore.Bands.Count - 1].Columns[1], UppercaseFirst(s));
                            scoreFinal = 0;
                        }
                    }
                }

                bgrvScore.EndUpdate();
                //Giao dien
                for (int i = 0; i < this.bgrvScore.Columns.Count; i++)
                {
                    this.bgrvScore.Columns[i].Visible = true;
                    this.bgrvScore.Columns[i].OptionsColumn.AllowEdit = false;
                    //this.bgrvScore.Columns[i].AppearanceHeader.Font.Bold = true;
                }
                //advBandedGridView1.Appearance.HeaderPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            //}
            bgrvScore.EndUpdate();
            #endregion
        }
        private void getSubjectClass()
        {
            if (lueCatalogue_Term.EditValue != null && lueCatalogue_SchoolYear.EditValue != null && lueCatalogue_Department.EditValue != null)
            {
                m_bds.DataSource = m_data.m_DCReadOnly.Training_Subject_Classes.GetSubjectClassList(m_data.m_DCReadOnly.Training_Subjects, m_data.m_DCReadOnly.Catalogue_Departments, lueCatalogue_Term.GetIntValue(),
                                            lueCatalogue_SchoolYear.GetIntValue(), lueCatalogue_Department.GetIntValue());
                lueTraining_Subject_Class.GetData(m_data.m_DCReadOnly.Training_Subject_Classes.GetSubjectClassList(m_data.m_DCReadOnly.Training_Subjects, m_data.m_DCReadOnly.Catalogue_Departments, lueCatalogue_Term.GetIntValue(),
                    lueCatalogue_SchoolYear.GetIntValue(), lueCatalogue_Department.GetIntValue()), displayMember: "ID");//"Name"
                m_dt = new DataTable();
                m_dt.Columns.Add("MSSV");
                m_dt.Columns.Add("HỌ LÓT");
                m_dt.Columns.Add("TÊN");
                m_dt.Columns.Add("NGÀY SINH");
                m_dt.Columns.Add("GIỚI TÍNH", System.Type.GetType("System.Boolean"));
                gctScore.DataSource = m_dt;
                this.bgrvScore.PopulateColumns();

                txtCredits.Text = null;
                txtLevel.Text = null;
                txtMajor.Text = null;
                txtSubject.Text = null;
                txtSubjectID.Text = null;
            }
        }
        private bool checkScoreType(List<int> scoreType, int subjectClass)
        {
            int k = 0;
            for (int i = 0; i < scoreType.Count; i++)
            {
                if (m_data.m_DCReadOnly.Training_Scores.CheckSubjectClassScoreType(subjectClass, scoreType[i]) == true)
                {
                    k++;
                }
            }
            if (k == scoreType.Count)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private GridBand Info(ref AdvBandedGridView advBandedGridView1)
        {
            GridBand grdBand = new GridBand();
            grdBand.Caption = "THÔNG TIN";

            BandedGridColumn clSTT = new BandedGridColumn();
            clSTT.Caption = "STT";
            clSTT.FieldName = "STT";
            clSTT.Width = 30;
            BandedGridColumn clStudentID = new BandedGridColumn();
            clStudentID.Caption = "MSSV";
            clStudentID.FieldName = "MSSV";
            clStudentID.Width = 80;
            //clStudentID.AppearanceCell.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            //clStudentID.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            
            BandedGridColumn clLastName = new BandedGridColumn();
            clLastName.Caption = "HỌ LÓT";
            clLastName.FieldName = "HỌ LÓT";
            clLastName.Width = 120;
            clLastName.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            BandedGridColumn clSureName = new BandedGridColumn();
            clSureName.Caption = "TÊN";
            clSureName.FieldName = "TÊN";
            clSureName.Width = 70;
            clSureName.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            BandedGridColumn clBirtDay = new BandedGridColumn();
            clBirtDay.Caption = "NGÀY SINH";
            clBirtDay.FieldName = "NGÀY SINH";
            clBirtDay.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            clBirtDay.DisplayFormat.FormatString = "d";
            clBirtDay.Width = 70;
            //clBirtDay.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;

            BandedGridColumn clGenDer = new BandedGridColumn();
            clGenDer.Caption = "GIỚI TÍNH";
            clGenDer.FieldName = "GIỚI TÍNH";
            //clGenDer.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            clGenDer.Width = 60;

            grdBand.Columns.Add(clSTT);
            grdBand.Columns.Add(clStudentID);
            grdBand.Columns.Add(clLastName);
            grdBand.Columns.Add(clSureName);
            grdBand.Columns.Add(clBirtDay);
            grdBand.Columns.Add(clGenDer);

            advBandedGridView1.Columns.Add(clSTT);
            advBandedGridView1.Columns.Add(clStudentID);
            advBandedGridView1.Columns.Add(clLastName);
            advBandedGridView1.Columns.Add(clSureName);
            advBandedGridView1.Columns.Add(clBirtDay);
            advBandedGridView1.Columns.Add(clGenDer);
            return grdBand;
        }
        private GridBand InSertScoreType(int _iScoreType, ref AdvBandedGridView advBandedGridView1,ref DataTable dt, int subjectClassKey)
        {            
            GridBand grdBand = new GridBand();
           // grdBand.Width = 300;

            var st = m_data.m_DCReadOnly.Training_ScoreTypes.Where(p => p.Training_ScoreTypeKey == _iScoreType).ToList();
            var percent = m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes.Where(p => p.Training_Subject_ClassKey == subjectClassKey && p.Training_ScoreTypeKey == _iScoreType).ToList();
            grdBand.Caption = st[0].Name.ToString().ToUpper()+"("+percent[0].DefaultPercent.ToString()+"%)";

            BandedGridColumn clMark_number = new BandedGridColumn();
            dt.Columns.Add(st[0].Name.ToString() + "ĐIỂM SỐ");
            clMark_number.Caption = "ĐIỂM SỐ";
            clMark_number.FieldName = st[0].Name.ToString() + "ĐIỂM SỐ";
            clMark_number.Width = 50;

            BandedGridColumn clMark_char = new BandedGridColumn();
            dt.Columns.Add(st[0].Name.ToString() + "ĐIỂM CHỮ");
            clMark_char.Caption = "ĐIỂM CHỮ";
            clMark_char.FieldName = st[0].Name.ToString() + "ĐIỂM CHỮ";
            clMark_char.Width = 80;

            BandedGridColumn clSignature = new BandedGridColumn();
            clSignature.Caption = "CHỮ KÝ";
            clSignature.Width = 65;

            BandedGridColumn clPages = new BandedGridColumn();
            dt.Columns.Add(st[0].Name.ToString() + "SỐ TỜ");
            clPages.Caption = "SỐ TỜ";
            clPages.FieldName = st[0].Name.ToString() + "SỐ TỜ";
            clPages.Width = 40;
            
            grdBand.Columns.Add(clPages);
            grdBand.Columns.Add(clMark_number);
            grdBand.Columns.Add(clMark_char);
            grdBand.Columns.Add(clSignature);

            advBandedGridView1.Columns.Add(clPages);
            advBandedGridView1.Columns.Add(clMark_number);
            advBandedGridView1.Columns.Add(clMark_char);
            advBandedGridView1.Columns.Add(clSignature);

            return grdBand;
        }
        private void FrmScore_SubTransScripts_Load(object sender, EventArgs e)
        {
            txtSubjectClassID.Focus();
        }
        private void txtSubjectClassID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnView_Click(sender, e);
        }
        private int getSubjectClassKey()
        {
            int sClassKey;
            if (lueTraining_Subject_Class.EditValue != null)
            {
                sClassKey = lueTraining_Subject_Class.GetIntValue();
            }
            else
            {
                Training_Subject_ClassInfo info = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
                sClassKey = info.Properties.Training_Subject_ClassKey;
            }
            return sClassKey;
        }
        private void txtSubjectClassID_EditValueChanged(object sender, EventArgs e)
        {
            getInfoFinal();
        }
        private string UppercaseFirst(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            char[] a = s.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }
    }
}